using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OpenNETCF.Net;
using System.Diagnostics;

namespace ServiceConnect
{
    public partial class View : Form
    {
        private Presenter m_presenter;

        public View()
        {
            InitializeComponent();
            m_presenter = new Presenter(this);

            entryList.SelectedIndexChanged += new EventHandler(entryList_SelectedIndexChanged);
            dialEntry.Click += new EventHandler(dialEntry_Click);
        }

        void dialEntry_Click(object sender, EventArgs e)
        {
            RasEntry selectedEntry = entryList.Items[entryList.SelectedIndices[0]].Tag as RasEntry;

            m_presenter.OnDial(selectedEntry);
        }

        void entryList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (entryList.SelectedIndices.Count <= 0)
            {
                m_presenter.OnClearEntrySelection();
            }
            else
            {
                m_presenter.OnSelectEntry(entryList.Items[entryList.SelectedIndices[0]].Tag as RasEntry);
            }
        }

        public void AddRasEntryToList(RasEntry entry)
        {
            ListViewItem lvi = new ListViewItem(new string[] {entry.Name, entry.Status.State.ToString(), entry.DeviceName});
            lvi.Tag = entry;
            entryList.Items.Add(lvi);
        }

        public void AddRasDeviceToList(DeviceInfo device)
        {
            ListViewItem lvi = new ListViewItem(new string[] { device.DeviceName, device.DeviceType });
            lvi.Tag = device;
            deviceList.Items.Add(lvi);
        }

        public void AddRasConnectionToList(RasConnection connection)
        {
            ListViewItem lvi = new ListViewItem(new string[] { connection.Name, connection.hConnection.ToString() });
            lvi.Tag = connection;
            connectionList.Items.Add(lvi);
        }

        public void SetConnectionList(RasConnection[] connections)
        {
            connectionList.BeginUpdate();
            connectionList.Items.Clear();

            foreach (RasConnection connection in connections)
            {
                ListViewItem lvi = new ListViewItem(new string[] { connection.Name, connection.hConnection.ToString() });
                lvi.Tag = connection;
                connectionList.Items.Add(lvi);                
            }
            connectionList.EndUpdate();
        }

        public void OnRasEntryStatusChange(RasConnection connection, RasConnState State, RasError ErrorCode)
        {
            foreach (ListViewItem item in entryList.Items)
            {
                RasEntry entry = item.Tag as RasEntry;
                if (entry.Name == connection.Name)
                {
                    item.SubItems[1].Text = State.ToString();

                    if (item.Selected)
                    {
                        entryStatus.Text = State.ToString();
                    }
                }
            }

            Debug.WriteLine(string.Format("'{0}' state changed to '{1}'", connection.Name, State.ToString()));
        }

        public void OnRasEntryError(RasEntry entry, RasError ErrorCode)
        {
            // TODO: update some UI element
        }

        public void ClearEntrySelection()
        {
            entryStatus.Text = "[select and entry]";
            dialEntry.Enabled = false;
        }

        public void ClearConnectionList()
        {
            connectionList.Items.Clear();
        }

        public void SelectEntry(RasEntry entry)
        {
            entryStatus.Text = entry.Status.State.ToString();
            dialEntry.Enabled = true;
        }

        public void EnableDialButton()
        {
            dialEntry.Enabled = true;
        }

        public void DisableDialButton()
        {
            dialEntry.Enabled = false;
        }
    }
}